<?php
session_start();
include_once '../pushing.php';

if ($pushTime == true) {
	if ($_SESSION['UserId'] != 3)
		header('Location: push.php');
}

if(!isset($_SESSION['UserId']))
{
	header('Location: login.php');
	die();
}

require_once '../Ajax_Requests/patientFile.php';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Save Information</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<script src="../JavaScript/jQueryTabs/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../JavaScript/spin.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../Styler.css"/>

<script type="text/javascript">

window.spinnerSaveFile;
function stopSaveSpinner() {
	spinnerSaveFile.stop();
}
window.spinnerListFile;
function stopListSpinner() {
	spinnerListFile.stop();
}

window.opts = {
		  lines: 16, // The number of lines to draw
		  length: 13, // The length of each line
		  width: 4, // The line thickness
		  radius: 8, // The radius of the inner circle
		  color: '#000', // #rgb or #rrggbb
		  speed: 1, // Rounds per second
		  trail: 60, // Afterglow percentage
		  shadow: false // Whether to render a shadow
		};


function deleteFile (fileNameDiv) {

	var information = fileNameDiv.split("|");
	var divName = information[0];
	var fileURL = information[1];
	
	var targetDelete = document.getElementById('listOfFiles');
	$.ajax({
		type: "POST",
		url: "../Ajax_Requests/patientFile.php",
		data: {
				PatId: <?php echo $_GET['PatId'];?>,
				method: "deletePatientFile",
				divID: divName,
				fileURL: fileURL
			},
		beforeSend: function () {
			spinnerListFile = new Spinner(opts).spin(targetDelete);
			},
		success: function (data) {
			
			stopListSpinner();
			
			if (data.length > 0) {
				
				information = data.split("|");
				//console.log(information);
				if (information[0] == "success") {
					//$("#" + information[1]).remove();
					listFiles();
				}
				else {
					//alert(information[1]);
				}
			} else {
				alert("ERROR: Could complete AJAX request - Delete File!");
			}
		}
	});
}

function createFile() {
	
	var targetSave = document.getElementById('login');
	$.ajax({
				type: "POST",
				url: "../Ajax_Requests/patientFile.php",
				data: {
						PatId: <?php echo $_GET['PatId'];?>,
						method: "savePatientFile"
					},
				beforeSend: function () {
					spinnerSaveFile = new Spinner(opts).spin(targetSave);
					},
				success: function (data) {
					
					stopSaveSpinner();
					var information;
					if (data.length > 0) {
						information = data.split("|");
						if (information[0] == "success") {
							$("#questionnaire").html(information[1]);
							$("#QuesButton").remove();
							listFiles();
						} else {
							console.log(data);
							alert(information[1]);
						}
					} else {
						alert("ERROR: Could complete AJAX request - Save File!");
					}
				}
	});
}

function listFiles() {
	var targetList = document.getElementById('listOfFiles');
	$.ajax({
		type: "POST",
		url: "../Ajax_Requests/patientFile.php",
		data: {
				PatId: <?php echo $_GET['PatId'];?>,
				method: "listPatientFiles"
			},
		beforeSend: function () {
			spinnerListFile = new Spinner(opts).spin(targetList);
			},
		success: function (data) {
			
			stopListSpinner();
			
			if (data.length > 0) {
				//console.log(data);
				$("#listOfFiles").html(data);
			} else {
				alert("ERROR: Could complete AJAX request - List Files!");
			}
		}
	});
}

window.onload = function () {
		
	listFiles();

};
		

</script>
</head>


<body>

<div id="header">
            <div class="pageTitleText">Save Information</div>
</div>
    
    <div id="mainMenu" >
    	<ul style="list-style: none outside none;">
    	     <li>
	        	<a href="choosePatient.php?SID=9"style="font-weight: bold; color: #7C3709;">Choose New Patient</a>
	        </li>
	        
	        <li>
	         	<a href="Add_ICD9.php?PatId=<?php echo $_GET['PatId'];?>">Patient ICD9</a>
	        </li>
	        
	        <li>
	         	<a href="Add Prescription.php?PatId=<?php echo $_GET['PatId'];?>">Patient Prescription</a>
	        </li>
	        
	        <li>
	        	<a href="Physical Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Physical Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Neuropsychological Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Neuropsychological Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Psychological Test.php?PatId=<?php echo $_GET['PatId'];?>">Psychological Test</a> 
	        </li>
	        
	        <li>
	        	<a href="Diagnostic Test.php?PatId=<?php echo $_GET['PatId'];?>">Diagnostic Test</a>
	        </li>
	        
	        <li>
	        	<a href="viewPatient.php?PatId=<?php echo $_GET['PatId'];?>">View Patient Information</a>
	        </li>
	       
	        <li>
	         	<a href="Edit Patient.php?PatId=<?php echo $_GET['PatId'];?>">Edit Patient Information</a>
	        </li>
	       
	       <li>
	        	<a href="Save Information.php?PatId=<?php echo $_GET['PatId'];?>" style="font-weight: bold; color: #FB7D08;">Save Patient Information</a>
	        </li>
	        
	       <li style="border-bottom: 1px solid #FD9D45;">
	       	 	<del><!--<a href="query.php">-->Query<!--</a>--></del>
	        </li>
	        
	        <li style="margin-top: 20px;">
	       	 	<a href="home.php">Home</a>
	        </li> 
	         
	        <li>
	       	 	<a href="logout.php">Logout</a>
	        </li>
	        
        </ul>
     </div>
    

<div class="formContainer">
<div id="login" style="top: 10px; margin: 0px; float: left;">
         	<div id="questionnaire"></div>
            <div class="mainbody">
            	<div id="QuesButton">
            	 <input type="button" value="Create File" class="loginButton" onclick="javascript: createFile()" style="margin-top: 0px;"/>
                </div>
           	</div>
</div>


<div class="Questions" style="width: 400px; min-height: 200px; top: 10px; margin-left: 500px;">
	<div style="margin: auto; width: 20%; margin-bottom: 20px;">Existing Files</div>
		<div class="mainbody" style="margin: auto; width: 90%;">
			<div id="listOfFiles">
			</div>
		</div>
	</div>
</div>
</body>
</html>